package com.yicong.ants.ui.scenic;

import android.view.View;

import com.cchao.simplelib.core.CollectionHelper;
import com.cchao.simplelib.core.JsonHelper;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.util.StringHelper;
import com.stay4it.banner.Banner;
import com.stay4it.banner.BannerConfig;
import com.stay4it.banner.listener.OnBannerListener;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.scenic.AddedItem;
import com.yicong.ants.bean.scenic.ScenicDetailBean;
import com.yicong.ants.databinding.AddedDetailActivityBinding;
import com.yicong.ants.manager.UserManager;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.holder.DetailBannerLoader;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.ui.common.ImageViewPagerActivity;
import com.yicong.ants.ui.order.BillHistoryTabActivity;
import com.yicong.ants.utils.AppUtils;
import com.yicong.ants.utils.TimeHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 二次消费详情页
 *
 * @author cchao
 * @version 2019-08-22.
 */
public class AddedDetailActivity extends BaseTitleBarActivity<AddedDetailActivityBinding> implements View.OnClickListener {
    AddedItem mAddedItem;
    ScenicDetailBean mScenicDetail;

    @Override
    protected int getLayout() {
        return R.layout.added_detail_activity;
    }

    @Override
    protected void initEventAndData() {
        mAddedItem = JsonHelper.toObject(getIntent().getStringExtra(Constants.Extra.JSON), AddedItem.class);
        mScenicDetail = JsonHelper.toObject(getIntent().getStringExtra(Constants.Extra.JSON2), ScenicDetailBean.class);
        setTitleText(mAddedItem.getProject_name());
        UiHelper.setDeleteLine(mDataBind.originalPrice);
        mDataBind.setBean(mAddedItem);
        mDataBind.setScenic(mScenicDetail);
        mDataBind.setClick(this);
        updateBanner();
    }

    private void updateBanner() {
        Banner banner = mDataBind.banner;
        //设置图片加载器
        banner.setImageLoader(new DetailBannerLoader());
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Router.turnTo(mContext, ImageViewPagerActivity.class)
                    .putExtra(Constants.Extra.JSON, JsonHelper.toJson(mAddedItem.getImages()))
                    .putExtra(Constants.Extra.Index, position)
                    .start();
            }
        });
        if (CollectionHelper.isEmpty(mAddedItem.getImages())) {
            Integer[] slideShowArray = new Integer[]{R.drawable.place_holder};
            banner.setImages(Arrays.asList(slideShowArray));
        } else {
            List<String> imgs = new ArrayList<>(mAddedItem.getImages().size());
            for (int i = 0, y = mAddedItem.getImages().size(); i < y; i++) {
                imgs.add(mAddedItem.getImages().get(i));
            }
            banner.setImages(imgs);
        }
        banner.start();
    }

    @Override
    protected void onLoadData() {

    }


    /**
     * 预定景区
     */
    void orderTime(String time) {
        Map<String, String> params = new HashMap<>();
        params.put("scenic_id", mScenicDetail.getScenic_id());
        params.put("project_id", mAddedItem.getId());
        params.put("visit_date", time);

        showProgress();
        addSubscribe(RetrofitHelper.getApis().submitProject(params)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                hideProgress();
                if (respBean.isCodeFail()) {
                    UiHelper.showLongToast(respBean.getMsg());
                    return;
                }
                UiHelper.showLongToast(respBean.getMsg());
                Router.turnTo(mContext, BillHistoryTabActivity.class)
                    .putExtra(Constants.Extra.Index, 1)
                    .start();
                finish();
            }, RxHelper.getHideProgressConsumer(this)));
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.apply:
                if (UserManager.isNotAuthElseTurn(mContext)) {
                    return;
                }
                // data 选择50
                Dialogs.showDataSelect(getFragmentManager(), str -> {
                    String format = "yyyy-MM-dd";

                    long selectStamp = 0;
                    try {
                        selectStamp = TimeHelper.formatToStamp(str, format);
                        if (selectStamp < TimeHelper.formatToStamp(mAddedItem.getStart_date(), format)) {
                            showToast("请选择合适的购买时间");
                            return;
                        }
                    } catch (Exception e) {
                        showToast(e.getMessage());
                        return;
                    }
                    orderTime(str);
                });

                break;
            case R.id.my_order:
                Router.turnTo(mContext, BillHistoryTabActivity.class)
                    .putExtra(Constants.Extra.Index, 1)
                    .start();
                break;
            case R.id.video_link_field:
                if (StringHelper.isEmpty(mAddedItem.getWeb_url())) {
                    return;
                }
                AppUtils.jumpToWeb(this, mAddedItem.getWeb_url());
                break;
        }
    }
}
